package com.demo.connectdbtest.module.xml.extract_py;

import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class py_rule_stat_extract {
    public static List<Integer> findAllPosition(String str, char ch) {
        List<Integer> positions = new ArrayList<>();
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == ch) {
                positions.add(i);
            }
        }
        return positions;
    }


    public static String extractCodeRuleNum(String rowContent){
        //rowContent="|convention |132    |NC       |NC         |";
        List<Integer> allPosition = findAllPosition(rowContent, '|');
        Integer endIndex = allPosition.get(2);
        Integer startIndex = allPosition.get(1);
        //System.out.println("查找字符串");
       // System.out.println(rowContent.substring(0,endIndex));
        return rowContent.substring(startIndex+1,endIndex);
    }

    public static void main(String[] args) {
        try {

            File file = new File("D:\\workData\\javaProject\\connectDB\\src\\main\\java\\com\\demo\\connectdbtest\\module\\xml\\extract_py\\后端代码检查.txt"); // 打开要读取的文件
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line;
            Boolean showFLag = false;
            ArrayList<String> contentList = new ArrayList<>();
            while ((line = reader.readLine()) != null) {
                if("Messages by category".equals(line)) {
                    showFLag = true;
                }
                if("% errors / warnings by module".equals(line)) {
                    showFLag = false;
                }
                if(showFLag) {
                    // 打印文件内容
                    // System.out.println(line);
                    contentList.add(line);
                }
            }
            reader.close();


            //遍历提取的文件内容
            Map<String, Integer> resultMap = new HashMap<>();
            for (String rowContent : contentList) {
                //System.out.println(rowContent);
                //System.out.println("===============");
                if(StringUtils.isEmpty(rowContent)) {
                    continue;
                }
                if(rowContent.startsWith("|convention")) {
                    String num = extractCodeRuleNum(rowContent);
                    if(StringUtils.isEmpty(num)) {
                        resultMap.put(CodeRuleTypeEnum.CONVENTION.getType(), 0);
                    } else {
                        resultMap.put(CodeRuleTypeEnum.CONVENTION.getType(), Integer.valueOf(num.trim()));
                    }
                }


                if(rowContent.startsWith("|refactor")) {
                    String num = extractCodeRuleNum(rowContent);
                    if(StringUtils.isEmpty(num)) {
                        resultMap.put(CodeRuleTypeEnum.REFACTOR.getType(), 0);
                    } else {
                        resultMap.put(CodeRuleTypeEnum.REFACTOR.getType(), Integer.valueOf(num.trim()));
                    }
                }

                if(rowContent.startsWith("|warning")) {
                    String num = extractCodeRuleNum(rowContent);
                    if(StringUtils.isEmpty(num)) {
                        resultMap.put(CodeRuleTypeEnum.WARNNING.getType(), 0);
                    } else {
                        resultMap.put(CodeRuleTypeEnum.WARNNING.getType(), Integer.valueOf(num.trim()));
                    }
                }

                if(rowContent.startsWith("|error")) {
                    String num = extractCodeRuleNum(rowContent);
                    if(StringUtils.isEmpty(num)) {
                        resultMap.put(CodeRuleTypeEnum.ERROR.getType(), 0);
                    } else {
                        resultMap.put(CodeRuleTypeEnum.ERROR.getType(), Integer.valueOf(num.trim()));
                    }
                }
            }

            //得出最后的结果
            for (Map.Entry<String, Integer> entry : resultMap.entrySet()) {
                System.out.println("key:" + entry.getKey() + " value:" + entry.getValue());
            }

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("文件读取错误：" + e.getMessage());
        }

    }
}
